home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / GRAHAM / XA_6S.ZIP / SOURCE / SHELLWRT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-24  |  6.2 KB  |  291 lines

  1. /*
  2.  * XaAES - XaAES Ain't the AES
  3.  *
  4.  * A multitasking AES replacement for MiNT
  5.  *
  6.  */
  7.  
  8. #include <OSBIND.H>
  9. #include <MINTBIND.H>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <memory.h>
  14. #include <basepage.h>
  15. #include "messages.h"
  16. #include "k_defs.h"
  17. #include "kernal.h"
  18. #include "xa_defs.h"
  19. #include "xa_types.h"
  20. #include "XA_GLOBL.H"
  21. #include "filesys.h"
  22.  
  23. void accstart(void);
  24. short init_moose(void);
  25.  
  26. short shell_write(short mode, short wisgr, short wiscr, char *parm, char *p_tail)
  27. {
  28.     short real_mode=mode&0xff,x_mode=mode&0xff00;
  29.     short child=0,t,alloced_cmd=FALSE;
  30.     char *run_path,*run_name,*save_tail,*save_cmd,*cmd,*ext;
  31.     char *tail=NULL;
  32.     XSHELW *x_shell=(XSHELW*)parm;
  33.     XA_CLIENT *new_client;
  34.  
  35.     if (p_tail)
  36.     {
  37.         tail=(char*)malloc(sizeof(char)*(p_tail[0]+2));
  38.         strncpy(tail,p_tail, p_tail[0]+1);
  39.         tail[tail[0]+1]='\0';
  40.     }
  41.  
  42.     if (!x_mode)
  43.     {
  44.     
  45.         if (parm)
  46.         {
  47.             cmd=(char*)malloc(strlen(parm)+2);
  48.             if (!cmd)
  49.                 return 0;
  50.             strcpy(cmd,parm);
  51.             alloced_cmd=TRUE;
  52.         }else{
  53.             cmd=parm;
  54.         }
  55.     
  56.     }else{
  57.     
  58.         if (x_shell->newcmd)
  59.         {
  60.             cmd=(char*)malloc(strlen(x_shell->newcmd)+2);
  61.             if (!cmd)
  62.                 return 0;
  63.             strcpy(cmd,x_shell->newcmd);
  64.             alloced_cmd=TRUE;
  65.         }else{
  66.             cmd=x_shell->newcmd;
  67.         }
  68.     }
  69.     
  70.     DIAGS(("shell_write():\n mode=%x\n cmd=%s\n tail=%s\n",mode,cmd,tail));
  71.  
  72.     if (real_mode<4)
  73.     {
  74.         save_cmd=(char*)malloc(strlen(cmd)+2);        /* Preserve the details */
  75.  
  76.         if (!save_cmd)
  77.         {
  78.             free(cmd);
  79.             return 0;
  80.         }
  81.  
  82.         strcpy(save_cmd,cmd);
  83.         save_tail=(char*)malloc(strlen(tail+1)+2);
  84.         strncpy(save_tail,tail, tail[0]+1);
  85.         save_tail[tail[0]+1]='\0';
  86.             
  87.         for(t=(short)strlen(cmd); (t>-1)&&(cmd[t]!='\\'); t--);    /* Seperate path & name*/
  88.         if (cmd[t]=='\\')
  89.         {
  90.             cmd[t]='\0';
  91.             run_path=cmd;
  92.             run_name=cmd+t+1;
  93.             if (run_path[1]==':')
  94.             {
  95.                 Dsetdrv((run_path[0] & ~32) - 'A');
  96.                 run_path+=2;
  97.             }
  98.             Dsetpath(run_path);    /* Ensure that client is run from it's correct directory */
  99.                                 /* - this allows it to find it's resources,etc */
  100.         }else{
  101.             run_name=cmd;
  102.             run_path=NULL;
  103.         }
  104.         
  105.         ext=run_name+strlen(run_name)-4;
  106.     }
  107.     
  108.     if (real_mode==0)
  109.     {
  110.         if (((stricmp(ext,".app"))&&(stricmp(ext,".prg")))==0) {
  111.             wisgr = 1;
  112.             real_mode=1;
  113.         }else if (((stricmp(ext,".acc"))&&(stricmp(ext,".acx")))==0) {
  114.             real_mode=3;
  115.         } else {
  116.             real_mode=1;
  117.             wisgr = 0;
  118.         }
  119.     }
  120.     
  121.     switch(real_mode)
  122.     {
  123.         case 1:
  124.             if (wisgr==0)        /* TOS Launch? Use TOSWIN... */
  125.             {
  126.                 int fd;
  127.                 char new_cmd[300];
  128.                     
  129.                 sprintf(new_cmd,"%s %s\\%s %s",run_path, run_path,run_name,tail+1);
  130.                 fd = (int)Fopen("U:\\PIPE\\TOSRUN", 2);
  131.                 t = (short)strlen(new_cmd) + 1;
  132.  
  133.                 Fwrite(fd, t, new_cmd);
  134.                     
  135.                 Fclose(fd);
  136.                     
  137.                 child=0;
  138.             }else{                /* GEM Launch */
  139.                 if (MOUSE_dev)
  140.                     Fclose(MOUSE_dev);        /* Kludge to get round a bug in MiNT (or moose?) */
  141.  
  142.                 child=(short)Pvfork();    /* Fork off a new process*/
  143.                 if (!child)
  144.                 {                        /* In child here */
  145. #if GENERATE_DIAGS
  146.                     Fforce(1,debug_file);            /* Redirect console output */
  147. #endif
  148.                     if (x_mode&SW_PSETLIMIT)        /* Limit child's memory? */
  149.                         Psetlimit(2,x_shell->psetlimit);
  150.                 
  151.                     if (x_mode&SW_UID)                /* XaAES extension - set user id */
  152.                         Psetuid(x_shell->uid);
  153.                         
  154.                     if (x_mode&SW_GID)                /* XaAES extension - set group id */
  155.                         Psetgid(x_shell->gid);
  156.  
  157.                     Pexec(200,run_name,tail,NULL);    /* Run the new client */
  158.     
  159.                     Pterm0();            /* If we reach here, we've failled to execute, so exit */
  160.                 }
  161.  
  162.                 if (x_mode&SW_PRENICE)                /* Was the child spawned as 'nice'? */
  163.                     Prenice(child, (short)x_shell->prenice);
  164.  
  165.                 new_client=Pid2Client(child);
  166.                 new_client->client_type=XA_CT_APP;
  167.                         
  168.                 new_client->cmd_name=save_cmd;
  169.                 new_client->cmd_tail=save_tail;
  170.                 new_client->parent=AESpid;
  171.  
  172. /*
  173.  * MASSIVE KLUDGE
  174.  * - for some reason, you MUST re-open the moose after a Pvfork()
  175.  */
  176.                 if (MOUSE_dev)
  177.                 {                                    
  178.                     MOOSE_DCLICK_COM dc_com;
  179.                     MOUSE_dev=Fopen("u:\\dev\\moose", O_RDWR);
  180.                     dc_com.dclick_prefix=MOOSE_DCLICK_PREFIX;
  181.                     dc_com.dclick_time=50;
  182.                     /*    dc_com.dclick_time=double_click_time;*/
  183.                     if (Fwrite(MOUSE_dev,4L,&dc_com)==0)
  184.                     {
  185.                         DIAGS(("moose set dclick time failled\n"));
  186.                     }
  187.                     
  188.                 }
  189.  
  190.             }
  191.  
  192.             Dsetdrv(XaAES_home_drv);
  193.             Dsetpath(XaAES_home);
  194.  
  195.             return child;
  196.             break;
  197.         case 3:
  198.             DIAGS(("launch acc[%s]\n",run_name));
  199.             if (MOUSE_dev)
  200.                 Fclose(MOUSE_dev);        /* Kludge to get round a bug in MiNT (or moose?) */
  201.  
  202.             {
  203.                 BASEPAGE* b;
  204.                 long p_rtn;
  205.                 char full_cmd[300];
  206.                     
  207.                 sprintf(full_cmd,"%s\\%s",run_path,run_name);
  208.                 p_rtn=Pexec(3, full_cmd, tail, NULL);
  209.                 
  210.                 if (p_rtn<0)
  211.                 {
  212.                     DIAGS(("launch failled:error=%ld\n",p_rtn));
  213.                     break;
  214.                 }
  215.                 
  216.                 b=(BASEPAGE*)p_rtn;
  217.                 
  218.                 Mshrink(b,256 + b->p_tlen + b->p_dlen + b->p_blen);
  219.  
  220.                 b->p_dbase = b->p_tbase;
  221.                 b->p_tbase = (char*)accstart;
  222.         
  223.                 child = (short)Pexec(104, run_name, b, NULL);
  224.                 
  225.                 DIAGS(("child=%d\n",child));
  226.             
  227.                 new_client=Pid2Client(child);
  228.                 new_client->client_type=XA_CT_ACC;
  229.             }
  230.  
  231.             if (MOUSE_dev)
  232.             {                                    /* Kludge - for some reason, you MUST re-open the moose after a Pvfork() */
  233.                 MOOSE_DCLICK_COM dc_com;
  234.  
  235.                 MOUSE_dev=Fopen("u:\\dev\\moose", O_RDWR);
  236.                 dc_com.dclick_prefix=MOOSE_DCLICK_PREFIX;
  237.                 dc_com.dclick_time=50;
  238.                 /*    dc_com.dclick_time=double_click_time;*/
  239.                 if (Fwrite(MOUSE_dev,4L,&dc_com)==0)
  240.                 {
  241.                     DIAGS(("moose set dclick time failled\n"));
  242.                 }
  243.             }
  244.  
  245.             break;
  246.         
  247.         case 4:        /* Shutdown system */
  248.             if (wisgr==2)
  249.                 shutdown=TRUE;
  250.             break;
  251.         
  252.         case 5:        /* Res change */
  253.             break;
  254.             
  255.         case 6:        /* undefined */
  256.             break;
  257.         
  258.         case 7:        /* broadcast message */
  259.             {
  260.                 XA_CLIENT *client;
  261.                 short *msg=(short*)parm;
  262.                 
  263.                 for(client=FirstClient(); client!=NULL; client=NextClient(client))
  264.                 {
  265.                     if (client->clnt_pipe_rd)
  266.                     {
  267.                         send_app_message(Client2Pid(client), msg[0], msg[1], msg[3], msg[4], msg[5], msg[6], msg[7]);
  268.                     }
  269.                 }
  270.             }
  271.             break;
  272.         
  273.         case 8:
  274.         case 9:
  275.         default:
  276.             break;
  277.  
  278.         
  279.     }
  280.     
  281.     Dsetdrv(XaAES_home_drv);
  282.     Dsetpath(XaAES_home);
  283.     
  284.     if (alloced_cmd)
  285.         free(cmd);
  286.     if (tail)
  287.         free(tail);
  288.     
  289.     return 0;
  290. }
  291.